/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: StandardService
 * @Package com.rx.core.service
 * @Description: 标准服务
 * @author: 陈锦韬
 * @date: 2021\6\9 0009
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rx.core.base.RxBaseData;
import com.rx.core.base.RxScanner;
import com.rx.core.data.UrlAnalyse;
import com.rx.core.dbase.RxBaseDbMust;
import com.rx.core.dbase.RxBaseMapper;
import com.rx.core.dbase.SplitListForSave;
import com.rx.core.dbase.mutil.param.*;
import com.rx.core.enm.HANDLER_TYPE;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.enm.RX_LOG_MODE;
import com.rx.core.iface.RelationForeignKey;
import com.rx.core.iface.RelationQuery;
import com.rx.core.iface.TreeNode;
import com.rx.core.mode.*;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: Administrator
 * @Description: 标准服务
 * @date: 2021\6\9 0009
 */
@Slf4j
@Service
public class StandardService extends RxBaseAbstractService {
    public static Integer RETURN_TREE = 1;
    public static Integer RETURN_TREE_PAGE = 2;

    private void setDefault(RxBaseData data,HANDLER_TYPE type){
        RxUtil.CheckUtil.check(data,RESULT_CODE.MESSAGE,"输入参数为空，不能操作数据库");
        if(EmptyChecker.isEmpty(data.getUrlAnalyse())){
            data.resetHandlerType(type);
            data.getHandlerController().onlyDefault();
        }
    }

    public Object detail(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_DETAIL);
        HandlerContainer.HandlerAllList handlerAllList = SingleDetailHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());
        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            if(data.isUseMybatisXml()){
                data.setResultData(mapper.search(data));
            }else {
                QueryWrapper queryWrapper = data.queryWrapper(false);
                data.setResultData(mapper.selectOne(queryWrapper));
            }
        });

        return data.getResultData();
    }

    public Object searchList(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_LIST);
        HandlerContainer.HandlerAllList handlerAllList = SingleListHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            if(data.isUseMybatisXml()){
                data.setResultData(mapper.searchList(data));
            }else {
                QueryWrapper queryWrapper = data.queryWrapper(false);
                data.setResultData(mapper.selectList(queryWrapper));
            }
        });

        if(RETURN_TREE.equals(data.getReturnType())){
            // 返回树形结构
            if(data.getResultData() instanceof List){
                data.setResultData(RxUtil.ListUtil.listToTree((List)data.getResultData(),null));
            }
        }


        return data.getResultData();
    }

    /**
     * 顶级分页。挂树
     * @return
     */
    private boolean topPageUnderTree(RxBaseData data,RxBaseMapper mapper){
        if(RETURN_TREE_PAGE.equals(data.getReturnType())){
            // 顶级节点分页的树形结构
            if(data.getResultData() instanceof IPage){
                IPage iPage = (IPage)data.getResultData();
                List<TreeNode> treeNodeList = (List<TreeNode>)iPage.getRecords();
                if (EmptyChecker.isEmpty(treeNodeList)){
                    return false;
                }
                List<String> rootCodeList = treeNodeList.stream().map(TreeNode::code).collect(Collectors.toList());
                List<TreeNode> childrenList = null;
                if(data.isUseMybatisXml()){
                    childrenList  = (List<TreeNode>)mapper.searchList(data);
                }else {
                    QueryWrapper queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("valid","0");
                    queryWrapper.in("root_code",rootCodeList);
                    childrenList = (List<TreeNode>)mapper.selectList(queryWrapper);
                }

                if (EmptyChecker.isEmpty(childrenList)){
                    return false;
                }
                // 将子节点挂上

                Map<String,List<TreeNode>> map = childrenList.stream().collect(Collectors.groupingBy(TreeNode::rootCode));

                for (TreeNode treeNode : treeNodeList) {
                    List<TreeNode> tmpList = map.get(treeNode.code());
                    if(EmptyChecker.notEmpty(tmpList)){
                        tmpList = RxUtil.ListUtil.listToTree(tmpList,null);
                        treeNode.childList().addAll(tmpList);
                    }
                }
            }
        }
        return false;
    }

    public Object searchPage(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_PAGE);
        HandlerContainer.HandlerAllList handlerAllList = SinglePageHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());
        // 获取Dao
        RxBaseMapper mapper = data.databaseMapper();
        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{

            if(data.isUseMybatisXml()){
                data.setResultData(mapper.searchList(data));
            }else {
                QueryWrapper queryWrapper = data.queryWrapper(false);
                Page page = new Page<>(data.getPageNum(), data.getPageSize());
                IPage iPage = mapper.selectPage(page, queryWrapper);
                data.setResultData(iPage);
            }
        });
        topPageUnderTree(data,mapper);
        return data.getResultData();
    }

    /**
     * 逻辑删除
     * @param data
     * @return
     */
    @Transactional
    public Object delete(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_DELETE);
        HandlerContainer.HandlerAllList handlerAllList = SingleDeleteHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            Integer integer = 0;
            if(data.isUseMybatisXml()){
                integer = mapper.delete(data);
            }else {
                QueryWrapper queryWrapper = data.queryWrapper(false);
                integer = mapper.delete(queryWrapper);
            }
            RxUtil.CheckUtil.condition(integer == 0, RESULT_CODE.MESSAGE,"删除失败");
            data.setResultData(integer);
        });

        return data.getResultData();
    }

    private void insertCore(RxBaseData data,RxBaseMapper mapper){
        Integer integer = 0;
        // 赋值基本用户信息
        data.assign(true);
        if(data.isUseMybatisXml()){
            integer = mapper.append(data);
        }else {
            integer = mapper.insert(data);
        }
        RxUtil.CheckUtil.condition(integer == 0, RESULT_CODE.MESSAGE,"新增失败");
        data.setResultData(integer);
    }
    /**
     * 新增
     * @param data
     * @return
     */
    @Transactional
    public Object insert(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_INSERT);
        HandlerContainer.HandlerAllList handlerAllList = SingleInsertHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            insertCore(data,mapper);
        });

        return data.getResultData();
    }

    /**
     * 单表更新
     * @param data
     * @return
     */
    @Transactional
    public Object update(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_UPDATE);
        HandlerContainer.HandlerAllList handlerAllList = SingleUpdateHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            Integer integer = 0;
            // 赋值基本用户信息
            data.assign(false);
            QueryWrapper queryWrapper = data.queryWrapper(true);

            if(data.isUseMybatisXml()){
                integer = mapper.update(data);
            }else {
                integer = mapper.update(data,queryWrapper);
            }
            RxUtil.CheckUtil.condition(integer == 0, RESULT_CODE.MESSAGE,"更新失败");
            data.setResultData(integer);
        });

        return data.getResultData();
    }

    public Object count(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_COUNT);
        HandlerContainer.HandlerAllList handlerAllList = SingleCountHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            if(data.isUseMybatisXml()){
                data.setResultData(mapper.count(data));
            }else {
                QueryWrapper queryWrapper = data.queryWrapper(false);
                data.setResultData(mapper.selectCount(queryWrapper));
            }
        });

        return data.getResultData();
    }

    public Object latest(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_LATEST);
        HandlerContainer.HandlerAllList handlerAllList = SingleLatestHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            if(data.isUseMybatisXml()){
                data.setResultData(mapper.latest(data));
            }else {
                QueryWrapper queryWrapper = data.queryWrapper(false);
                // 用主键进行排序会比较快
                queryWrapper.orderByDesc(RxBaseDbMust.ID);
                // 只能用于mysql数据库 其他数据库需要自定义覆盖处理器实现
                // 如果想取出多条记录，也需要自定义覆盖处理器实现。
                queryWrapper.last("limit 1");
                data.setResultData(mapper.selectOne(queryWrapper));
            }
        });

        return data.getResultData();
    }

    public Object statis(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_STATIS);
        HandlerContainer.HandlerAllList handlerAllList = SingleStatisHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            if(data.isUseMybatisXml()){
                data.setResultData(mapper.statis(data));
            }else {
                QueryWrapper queryWrapper = data.queryWrapperForStatis();
                data.setResultData(mapper.selectList(queryWrapper));
            }
        });

        return data.getResultData();
    }

    @Transactional
    public Object batchInsert(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_BATCH_INSERT);
        HandlerContainer.HandlerAllList handlerAllList = SingleBatchInsertHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            Integer integer = 0;

            if(data.isUseMybatisXml()){
                integer = data.batch(null,true,(e)->mapper.batchInsert((List)e));
            }else {
                integer = data.batch(null,true,(e)->mapper.batchAppend((List)e));
            }
            RxUtil.CheckUtil.condition(integer != data.getDataList().size(), RESULT_CODE.MESSAGE,"批量新增失败");
            data.setResultData(integer);
        });

        return data.getResultData();
    }

    @Transactional
    public Object batchUpdate(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_BATCH_UPDATE);
        HandlerContainer.HandlerAllList handlerAllList = SingleBatchUpdateHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            RxBaseMapper mapper = data.databaseMapper();
            Integer integer = 0;
            if(data.isUseMybatisXml()){
                integer = data.batch(null,false,(e)->mapper.batchUpdate((List)e));
            }else {
                integer = data.batch(null,false,(e)->mapper.batchUpdateNoXml((List)e));
            }

            RxUtil.CheckUtil.condition(integer != data.getDataList().size(), RESULT_CODE.MESSAGE,"批量更新失败");
            data.setResultData(integer);
        });
        return data.getResultData();
    }

    @Transactional
    public Object batchDelete(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_BATCH_DELETE);
        HandlerContainer.HandlerAllList handlerAllList = SingleBatchDeleteHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            RxBaseMapper mapper = data.databaseMapper();
            Integer integer = 0;
            if(data.isUseMybatisXml()){
                integer = data.batch(null,false,(e)->mapper.batchDelete((List)e));
            }else {
                integer = data.batch(null,false,(e)->mapper.batchDeleteNoXml((List)e));
            }

            RxUtil.CheckUtil.condition(integer != data.getDataList().size(), RESULT_CODE.MESSAGE,"批量删除失败");
            data.setResultData(integer);
        });
        return data.getResultData();
    }

    @Transactional
    public Object batchSave(RxBaseData data){
        setDefault(data,HANDLER_TYPE.SINGLE_BATCH_SAVE);
        HandlerContainer.HandlerAllList handlerAllList = SingleBatchSaveHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            SplitListForSave forSave
                    = data.splitListForSave();
            RxBaseMapper mapper = data.databaseMapper();
            Integer integer = 0;
            // 分辨出 新增、更新、删除 的记录
            if(data.isUseMybatisXml()){
                if (EmptyChecker.notEmpty(forSave.getAddList())){
                    integer = data.batch(forSave.getAddList(),null,true,(e)->mapper.batchInsert((List)e));
                }
                if (EmptyChecker.notEmpty(forSave.getModifyList())){
                    integer += data.batch(forSave.getModifyList(),null,false,(e)->mapper.batchUpdate((List)e));
                }
                if (EmptyChecker.notEmpty(forSave.getDeleteList())){
                    integer += data.batch(forSave.getDeleteList(),null,false,(e)->mapper.batchDelete((List)e));
                }

            }else {
                if (EmptyChecker.notEmpty(forSave.getAddList())){
                    integer = data.batch(forSave.getAddList(),null,true,(e)->mapper.batchAppend((List)e));
                }
                if (EmptyChecker.notEmpty(forSave.getModifyList())){
                    integer += data.batch(forSave.getModifyList(),null,false,(e)->mapper.batchUpdateNoXml((List)e));
                }
                if (EmptyChecker.notEmpty(forSave.getDeleteList())){
                    integer += data.batch(forSave.getDeleteList(),null,false,(e)->mapper.batchDeleteNoXml((List)e));
                }
            }

            RxUtil.CheckUtil.condition(integer != data.getDataList().size(), RESULT_CODE.MESSAGE,"批量保存失败");
            data.setResultData(integer);
        });
        return data.getResultData();
    }

    @Transactional
    public Object noDatabase(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = CommonNoDatabaseHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            data.setResultData(1);
        });
        return data.getResultData();
    }


    public Object innerSearchList(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = DoubleInnerListHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            data.setResultData(mapper.innerSearchList(DoubleInnerParameter.build(data)));
        });

        if(RETURN_TREE.equals(data.getReturnType())){
            // 返回树形结构
            if(data.getResultData() instanceof List){
                data.setResultData(RxUtil.ListUtil.listToTree((List)data.getResultData(),null));
            }
        }


        return data.getResultData();
    }
    public Object unionSearchList(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = DoubleUnionInnerListHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        RxUtil.CheckUtil.check(((RxBaseData) ((RelationQuery) data).obtainRelation()).getReturnType(), RESULT_CODE.MESSAGE,"returnType需要设置值");

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            data.setResultData(mapper.unionSearchList(DoubleUnionInnerParameter.build(data)));
        });

        if(RETURN_TREE.equals(data.getReturnType())){
            // 返回树形结构
            if(data.getResultData() instanceof List){
                data.setResultData(RxUtil.ListUtil.listToTree((List)data.getResultData(),null));
            }
        }

        return data.getResultData();
    }

    public Object unionSearchPage(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = DoubleUnionInnerPageHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());
        RxUtil.CheckUtil.check(((RxBaseData) ((RelationQuery) data).obtainRelation()).getReturnType(), RESULT_CODE.MESSAGE,"returnType需要设置值");

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            Page page = new Page<>(data.getPageNum(), data.getPageSize());
            IPage iPage = mapper.unionSearchPage(page, DoubleUnionInnerParameter.build(data));
            data.setResultData(iPage);
        });


        return data.getResultData();
    }

    public Object unionLeftSearchList(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = DoubleUnionLeftListHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());
        RxUtil.CheckUtil.check(((RxBaseData) ((RelationQuery) data).obtainRelation()).getReturnType(), RESULT_CODE.MESSAGE,"returnType需要设置值");

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            data.setResultData(mapper.unionLeftSearchList(DoubleUnionLeftParameter.build(data)));
        });

        if(RETURN_TREE.equals(data.getReturnType())){
            // 返回树形结构
            if(data.getResultData() instanceof List){
                data.setResultData(RxUtil.ListUtil.listToTree((List)data.getResultData(),null));
            }
        }

        return data.getResultData();
    }

    public Object unionLeftSearchPage(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = DoubleUnionLeftPageHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());
        RxUtil.CheckUtil.check(((RxBaseData) ((RelationQuery) data).obtainRelation()).getReturnType(), RESULT_CODE.MESSAGE,"returnType需要设置值");

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            Page page = new Page<>(data.getPageNum(), data.getPageSize());
            IPage iPage = mapper.unionLeftSearchPage(page, DoubleUnionLeftParameter.build(data));
            data.setResultData(iPage);
        });


        return data.getResultData();
    }

    public Object leftSearchList(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = DoubleLeftListHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            data.setResultData(mapper.leftSearchList(DoubleLeftJoinParameter.build(data)));
        });

        if(RETURN_TREE.equals(data.getReturnType())){
            // 返回树形结构
            if(data.getResultData() instanceof List){
                data.setResultData(RxUtil.ListUtil.listToTree((List)data.getResultData(),null));
            }
        }


        return data.getResultData();
    }

    public Object innerSearchPage(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = DoubleInnerPageHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());
        // 获取Dao
        RxBaseMapper mapper = data.databaseMapper();
        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            Page page = new Page<>(data.getPageNum(), data.getPageSize());
            IPage iPage = mapper.innerSearchPage(page, DoubleInnerParameter.build(data));
            data.setResultData(iPage);
        });

        topPageUnderTree(data,mapper);
        return data.getResultData();
    }

    public Object leftSearchPage(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = DoubleLeftPageHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());
        RxBaseMapper mapper = data.databaseMapper();
        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            Page page = new Page<>(data.getPageNum(), data.getPageSize());
            IPage iPage = mapper.leftSearchPage(page, DoubleLeftJoinParameter.build(data));
            data.setResultData(iPage);
        });

        topPageUnderTree(data,mapper);


        return data.getResultData();
    }

    private void insertSingleSlave(RxBaseData data){
        String table[] = data.getUrlAnalyse().getService().split("_");
        Class secondTable = RxScanner.getClassByName(table[0]);
        RelationQuery relationQuery = (RelationQuery)data;
        RelationForeignKey relationForeignKey = (RelationForeignKey) relationQuery.obtainRelation();
        if (EmptyChecker.isEmpty(relationForeignKey)){
            return;
        }
        relationForeignKey.assignItemKey(((RxBaseDbMust) data).getBid());
        String formName = RxUtil.StringUtil.firstUpperHumpToDash(secondTable.getSimpleName());
        ((RxBaseData)relationForeignKey).setUrlAnalyse(UrlAnalyse.create(HANDLER_TYPE.SINGLE_INSERT.getUrlByMode(formName)));
        Object count = insert((RxBaseData)relationForeignKey);
        RxUtil.CheckUtil.check(count,RESULT_CODE.MESSAGE,"插入附表时失败");
    }
    private void insertBatchSlave(RxBaseData data){
        String table[] = data.getUrlAnalyse().getService().split("_");
        Class secondTable = RxScanner.getClassByName(table[0]);
        for (RxBaseData rxBaseData : data.getDataList()) {
            RelationQuery relationQuery = (RelationQuery)rxBaseData;
            ((RelationForeignKey) relationQuery.obtainRelation()).assignItemKey(((RxBaseDbMust) rxBaseData).getBid());
        }

        try {
            RxBaseData condition = RxBaseData.create(secondTable, HANDLER_TYPE.SINGLE_INSERT);
            condition.setDataList(data.getDataList());
            Object count = batchInsert(condition);
            RxUtil.CheckUtil.check(count,RESULT_CODE.MESSAGE,"批量插入附表时失败");
        } catch (IllegalAccessException | InstantiationException e) {
            RxUtil.CheckUtil.throwOut(RESULT_CODE.MESSAGE,"批量插入附表时失败");
        }
    }
    @Transactional
    public Object doubleInsert(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = DoubleInsertHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());
        RxBaseMapper mapper = data.databaseMapper();
        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            insertCore(data,mapper);
            if (EmptyChecker.isEmpty(data.getDataList())){
                insertSingleSlave(data);
            }else{
                insertBatchSlave(data);
            }
        });

        return data.getResultData();
    }

    @Transactional
    public Object copyInsert(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = DoubleCopyHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 查询 从表
            RelationQuery relationQuery = (RelationQuery)data;
            RxBaseData slave = (RxBaseData) relationQuery.obtainRelation();
            String table[] = data.getUrlAnalyse().getService().split("_");
            Class secondTable = RxScanner.getClassByName(table[0]);
            String formName = RxUtil.StringUtil.firstUpperHumpToDash(secondTable.getSimpleName());
            slave.setUrlAnalyse(UrlAnalyse.create(HANDLER_TYPE.SINGLE_LIST.getUrlByMode(formName)));
            RxBaseMapper mapper = slave.databaseMapper();
            mapper.copyInsert(DoubleCopyInsertParameter.build(data)) ;
            // 插入 从表
        });

        return data.getResultData();
    }

    public Object threeInnerSearchList(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = ThreeInnerListHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());

        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{
            // 获取Dao
            RxBaseMapper mapper = data.databaseMapper();
            data.setResultData(mapper.threeInnerSearchList(ThreeInnerParameter.build(data)));
        });

        if(RETURN_TREE.equals(data.getReturnType())){
            // 返回树形结构
            if(data.getResultData() instanceof List){
                data.setResultData(RxUtil.ListUtil.listToTree((List)data.getResultData(),null));
            }
        }


        return data.getResultData();
    }

    public Object threeInnerLeftPage(RxBaseData data){

        HandlerContainer.HandlerAllList handlerAllList = ThreeInnerLeftPageHandlerContainer.MEMMORY.getHandlerList(
                data.getUrlAnalyse().getUrl());
        // 获取Dao
        RxBaseMapper mapper = data.databaseMapper();
        // 前 覆盖 后 处理器处理模式
        handlerMode(data,handlerAllList,()->{

            Page page = new Page<>(data.getPageNum(), data.getPageSize());
            IPage iPage = mapper.threeInnerLeftPage(page, ThreeInnerLeftPageParameter.build(data));
            data.setResultData(iPage);
        });

        topPageUnderTree(data,mapper);

        return data.getResultData();
    }
}
